parameters:
  buildConfig: ''
  archType: ''
  osGroup: ''
  osSubgroup: ''
  container: ''
  testGroup: ''
  displayNameArgs: ''
  condition: true
  variables: {}
  pool: ''
  runtimeFlavor: 'coreclr'
  runtimeVariant: ''
  dependsOn: []
  crossBuild: false

### Build managed test components (native components are getting built as part
### of the product build job).

jobs:
- template: /eng/pipelines/common/templates/runtimes/xplat-job.yml
  parameters:
    buildConfig: ${{ parameters.buildConfig }}
    archType: ${{ parameters.archType }}
    osGroup: ${{ parameters.osGroup }}
    osSubgroup: ${{ parameters.osSubgroup }}
    crossBuild: ${{ parameters.crossBuild }}
    container: ${{ parameters.container }}
    runtimeVariant: ${{ parameters.runtimeVariant }}
    testGroup: ${{ parameters.testGroup }}
    pool: ${{ parameters.pool }}

    # Test jobs should continue on error for internal builds
    ${{ if eq(variables['System.TeamProject'], 'internal') }}:
      continueOnError: true

    # Compute job name from template parameters
    ${{ if in(parameters.testGroup, 'innerloop', 'clrinterpreter') }}:
      name: '${{ parameters.runtimeFlavor }}_common_test_build_p0_AnyOS_AnyCPU_${{ parameters.buildConfig }}'
      displayName: '${{ parameters.runtimeFlavor }} Common Pri0 Test Build AnyOS AnyCPU ${{ parameters.buildConfig }}'
    ${{ if notIn(parameters.testGroup, 'innerloop', 'clrinterpreter') }}:
      name: '${{ parameters.runtimeFlavor }}_common_test_build_p1_AnyOS_AnyCPU_${{ parameters.buildConfig }}'
      displayName: '${{ parameters.runtimeFlavor }} Common Pri1 Test Build AnyOS AnyCPU ${{ parameters.buildConfig }}'

    # Since the condition is being altered, merge the default with the additional conditions.
    # See https://docs.microsoft.com/azure/devops/pipelines/process/conditions
    condition: and(succeeded(), ${{ parameters.condition }})

    ${{ if ne(parameters.dependsOn[0], '') }}:
      dependsOn: ${{ parameters.dependsOn }}

    ${{ if in(parameters.testGroup, 'innerloop', 'clrinterpreter') }}:
      timeoutInMinutes: 90

    ${{ if notIn(parameters.testGroup, 'innerloop', 'clrinterpreter') }}:
      timeoutInMinutes: 160

    variables:
      - ${{ each variable in parameters.variables }}:
        - ${{ variable }}
      - name: liveRuntimeBuildParams
        value: 'libs+clr.iltools -c Release -ci'

      - name: runtimeFlavorArgs
        value: ''

      - ${{ if eq(parameters.runtimeFlavor, 'mono') }}:
        - name: runtimeFlavorArgs
          value: '-mono'

      - ${{ if and(eq(parameters.runtimeFlavor, 'coreclr'), in(parameters.osGroup, 'ios', 'iossimulator', 'tvos', 'tvossimulator', 'maccatalyst')) }}:
        - name: runtimeFlavorArgs
          value: '-nativeaot'

      - name: testTreeFilterArg
        value: ''

      # Only build GCSimulator tests when the gc-simulator group is specified.
      - ${{ if eq(parameters.testGroup, 'gc-simulator') }}:
          - name: testTreeFilterArg
            value: 'tree GC/Scenarios/GCSimulator'

      - template: /eng/pipelines/common/templates/runtimes/native-test-assets-variables.yml
        parameters:
          runtimeFlavor: coreclr
          testGroup: ${{ parameters.testGroup }}
          liveLibrariesBuildConfig: ${{ parameters.liveLibrariesBuildConfig }}

    steps:

    # Install test build dependencies
    - ${{ if in(parameters.osGroup, 'osx', 'maccatalyst', 'ios', 'iossimulator', 'tvos', 'tvossimulator') }}:
      - script: $(Build.SourcesDirectory)/eng/install-native-dependencies.sh $(osGroup)
        displayName: Install native dependencies

    # Build core/libraries dependencies of test build
    - script: $(Build.SourcesDirectory)/build$(scriptExt) $(liveRuntimeBuildParams) $(crossArg)
      displayName: Build coreclr/libs components needed by test build

    - ${{ if in(parameters.osGroup, 'osx', 'ios', 'tvos') }}:
      - script: |
          du -sh $(Build.SourcesDirectory)/*
          df -h
        displayName: Disk Usage before Build

    # Build managed test components
    - script: $(Build.SourcesDirectory)/src/tests/build$(scriptExt) $(logRootNameArg)Managed allTargets skipnative skipgeneratelayout skiptestwrappers $(buildConfig) $(archType) $(runtimeFlavorArgs) $(crossArg) $(priorityArg) $(testTreeFilterArg) ci /p:TargetOS=AnyOS
      displayName: Build managed test components

    - ${{ if in(parameters.osGroup, 'osx', 'ios', 'tvos') }}:
      - script: |
          du -sh $(Build.SourcesDirectory)/*
          df -h
        displayName: Disk Usage after Build

    # Zip and publish managed test components
    - template: /eng/pipelines/common/upload-artifact-step.yml
      parameters:
        rootFolder: '$(binTestsPath)/AnyOS.$(archType).$(buildConfigUpper)'
        includeRootFolder: false
        archiveExtension: '.tar.gz'
        archiveType: tar
        tarCompression: gz
        artifactName: $(managedGenericTestArtifactName)
        displayName: 'managed test components (generic)'


    # Publish .packages/microsoft.net.sdk.il needed for traversing
    # test projects during the copynativeonly command in run test job.
    - template: /eng/pipelines/common/upload-artifact-step.yml
      parameters:
        rootFolder: $(microsoftNetSdkIlFolderPath)
        includeRootFolder: false
        archiveType: tar
        tarCompression: gz
        archiveExtension: '.tar.gz'
        artifactName: $(microsoftNetSdkIlArtifactName)
        displayName: 'Microsoft.NET.Sdk.IL package'


    # Publish Logs
    - task: PublishPipelineArtifact@1
      displayName: Publish Logs
      inputs:
        targetPath: $(Build.SourcesDirectory)/artifacts/log
        artifactName: '${{ parameters.runtimeFlavor }}_Common_Runtime_TestBuildLogs_Attempt$(System.JobAttempt)_AnyOS_AnyCPU_$(buildConfig)_${{ parameters.testGroup }}'
      continueOnError: true
      condition: always()
